package com.zzxx.wechat.common;


import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

@Component
@Data
public class BrdProxyProperty {

    private String host;

    private Integer port;

    private String user;

    private String password;

    @PostConstruct
    private void init() throws Exception {

        String configPath = System.getProperty("proxy.config");
        Properties properties = new Properties();
        if (StringUtils.isEmpty(configPath)){
            // 从磁盘路径读取文件
            try (InputStream is =BrdProxyProperty.class.getClassLoader().getResourceAsStream("brd-proxy.properties")) {
                properties.load(is);
            }
        }else {
            // 从磁盘路径读取文件
            try (InputStream is = Files.newInputStream(Paths.get(configPath))) {
                properties.load(is);
            }
        }

        this.host = properties.getProperty("host");
        this.port = Integer.parseInt(properties.getProperty("port"));

        this.user = properties.getProperty("user");
        this.password = properties.getProperty("password");
    }
}
